# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time

# from jms.dm.dm_collect_analysis import jms_dm__collect_analysis
from utils.alerts.doris_telnet_check import net_is_used
from utils.operators.rich_sql_sensor import RichSqlSensor

label = f"dwd_export_collect_analysis_detail_{int(time())}"
timeout = timedelta(minutes=60).seconds

# from airflow.operators.dummy_operator import DummyOperator
# doris_jms_dwd__dwd_export_collect_analysis_detail = DummyOperator(
#     task_id='doris_jms_dwd__dwd_export_collect_analysis_detail',
#     email='chenhongping@yl-scm.com',
#     retries=0,
#     priority_weight=0,
#     # sla=timedelta(hours=2),
# )
dorisConn = net_is_used()
doris_jms_dwd__dwd_export_collect_analysis_detail = RichSqlSensor(
    task_id='doris_jms_dwd__dwd_export_collect_analysis_detail',
    email=['zhangqinglin@jtexpress.com','yl_bigdata@yl-scm.com'],
    conn_id='new_doris',
    priority_weight=5,
    pool='broker_load_pool',
    pre_sql=f"""
             LOAD LABEL jms_dwd.{label} (
                 DATA INFILE("hdfs://yl-bg-hdp/dw/hive/jms_dm.db/external/dm_collect_analysis_detail/dt={{{{ execution_date | cst_ds }}}}/*")
                 INTO TABLE dwd_collect_analysis_detail
                 FORMAT AS 'PARQUET'
             )
             WITH BROKER '{{{{var.json.doris_brokers | random_choice}}}}'
             PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dwd WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dwd WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120),
    sla=timedelta(hours=8),
)

